package com.yixq.soa.service.user.aop;

import com.yixq.soa.service.web.service.DataValidService;
import com.yixq.soa.service.web.utils.BaseController;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * Created by zjp on 2017/5/16.
 */
@Aspect
@Component
public class ReqValidAop {

    @Pointcut(value = "execution(public org.springframework.http.ResponseEntity<Object> com.yixq.soa.service.user.controller..*.*(..)) && @annotation(io.swagger.annotations.ApiOperation)")
    public void userController() {
    }

    @Around(value = "userController()")
    public Object doValid(ProceedingJoinPoint joinPoint) throws Throwable {
        for (Object args : joinPoint.getArgs()) {
            String errMsg = DataValidService.checkData(args);
            if (StringUtils.isNotBlank(errMsg)) {
                return ((BaseController) joinPoint.getThis()).checkParamError(errMsg);
            }
        }
        return joinPoint.proceed(joinPoint.getArgs());
    }
}
